<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg"%>
<%
	String path=request.getContextPath();
	String basePath=request.getScheme()+"://"
			+request.getServerName()+":"+request.getServerPort()
			+path+"/";
%>
<!DOCTYPE html>
<html>
<base href="<%=basePath%>">
<head>
	<title>题目类别管理</title>
	<meta charset="utf-8">
	<link rel="icon" href="img/logo.png" sizes="32x32">
	<link rel="stylesheet" type="text/css" href="layui/css/layui.css">
	<script type="text/javascript" src="layui/layui.js"></script>
</head>
<body>
	<div class="layui-anim layui-anim-up">
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			<legend>题目难度</legend>
		</fieldset>
		<table class="layui-hide" id="test" lay-filter="demo"></table>
		<script type="text/html" id="toolbarDemo">
		  <div class="layui-btn-container">
		    <button class="layui-btn layui-btn-sm" lay-event="addLevel" id="addLevel">添加难度</button>
		  </div>
		</script>
		<script type="text/html" id="barDemo">
	  		<a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
	  		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		</script>
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			<legend>题目类别</legend>
		</fieldset>
		<table class="layui-hide" id="test1" lay-filter="demo1"></table>
		<script type="text/html" id="toolbarDemo1">
		  <div class="layui-btn-container">
		    <button class="layui-btn layui-btn-sm" lay-event="addType" id="addType">添加类别</button>
		  </div>
		</script>
		<script type="text/html" id="barDemo1">
	  		<a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
	  		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		</script>
	</div>
	<script>
		layui.use('table', function(){
		  var table = layui.table;
		  var $=layui.$;
		  table.render({
		    elem: '#test'
		    ,toolbar: '#toolbarDemo'
		    ,url:'problem/problemLevelList'
		    ,cols: [[
		      {field:'problemLevelId', width:"10%", title: '编号', sort: true}
		      ,{field:'problemLevelName', width:"70%", title: '难度名称',edit: 'text'}
		      ,{fixed:'right', title:'操作', toolbar: '#barDemo', width:"20%"}
		    ]]
		    ,page: true
		  });
		  //修改和删除题目难度
		  table.on('tool(demo)', function(obj){
			    var data = obj.data;
			   	if(obj.event === 'del'){
			      layer.confirm('真的删除么', function(index){
			      	obj.del();
			        layer.close(index);
			        $.ajax({
			            type : "POST",
			            dataType:"json",
			            contentType: "application/json;charset=UTF-8",
			            url : "problem/deleteProblemLevel",
			            async: false,
			            data : JSON.stringify(data),
			            success : function(result) {
			            	
			            },
			            error : function(e){
			                console.log(e.status);
			                console.log(e.responseText);
			            }
			        });
			      });
			    } else if(obj.event === 'edit'){
			    	 table.on('edit(demo)', function(obj){
			 		    var value = obj.value //得到修改后的值
			 		    ,data = obj.data //得到所在行所有键值
			 		    ,field = obj.field; //得到字段
			 		   // layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
			 		 });
			    	 layer.confirm('确认修改吗', function(index){
					        layer.close(index);
					        $.ajax({
					            type : "POST",
					            dataType:"json",
					            contentType: "application/json;charset=UTF-8",
					            url : "problem/updateProblemLevel",
					            async: false,
					            data : JSON.stringify(data),
					            success : function(result) {
					            	
					            },
					            error : function(e){
					                console.log(e.status);
					                console.log(e.responseText);
					            }
					        });
					   });
			    	}
			  });
			//添加难度
		  $("#addLevel").click(function() {
			  layer.open({
				  content: '<input type="text" name="problemLevelName" id="problemLevelName" class="layui-input" placeholder="输入题目难度"/>',
				  yes: function(index, layero){
					   var data={problemLevelName:$("#problemLevelName").val()};
					   $.ajax({
				            type : "POST",
				            dataType:"json",
				            contentType: "application/json;charset=UTF-8",
				            url : "problem/addProblemLevel",
				            async: false,
				            data : JSON.stringify(data),
				            success : function(result) {
				            	location.reload();
				            },
				            error : function(e){
				                console.log(e.status);
				                console.log(e.responseText);
				            }
				        });
					   layer.close(index);
				  }
			  });
		  });

		  table.render({
		    elem: '#test1'
		    ,toolbar: '#toolbarDemo1'
		    ,url:'problem/problemTypeList'
		    ,cols: [[
		      {field:'problemTypeId', width:"10%", title: '编号', sort: true}
		      ,{field:'problemTypeName', width:"70%", title: '类型名称',edit: 'text'}
		      ,{fixed:'right', title:'操作', toolbar: '#barDemo1', width:"20%"}
		    ]]
		  	,limit: 10
		    ,page: true
		  });
		  //添加类型
		  $("#addType").click(function() {
			  layer.open({
				  content: '<input type="text" name="problemTypeName" id="problemTypeName" class="layui-input" placeholder="输入题目类型"/>',
				  yes: function(index, layero){
					   var data={problemTypeName:$("#problemTypeName").val()};
					   $.ajax({
				            type : "POST",
				            dataType:"json",
				            contentType: "application/json;charset=UTF-8",
				            url : "problem/addProblemType",
				            async: false,
				            data : JSON.stringify(data),
				            success : function(result) {
				            	location.reload();
				            },
				            error : function(e){
				                console.log(e.status);
				                console.log(e.responseText);
				            }
				        });
					   layer.close(index);
				  }
			  });
		  });
		  //修改和删除题目类型
		  table.on('tool(demo1)', function(obj){
			    var data = obj.data;
			   	if(obj.event === 'del'){
			      layer.confirm('真的删除么', function(index){
			      	obj.del();
			        layer.close(index);
			        $.ajax({
			            type : "POST",
			            dataType:"json",
			            contentType: "application/json;charset=UTF-8",
			            url : "problem/deleteProblemType",
			            async: false,
			            data : JSON.stringify(data),
			            success : function(result) {
			            	
			            },
			            error : function(e){
			                console.log(e.status);
			                console.log(e.responseText);
			            }
			        });
			      });
			    } else if(obj.event === 'edit'){
			    	 table.on('edit(demo)', function(obj){
			 		    var value = obj.value //得到修改后的值
			 		    ,data = obj.data //得到所在行所有键值
			 		    ,field = obj.field; //得到字段
			 		   // layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
			 		 });
			    	 layer.confirm('确认修改吗', function(index){
					        layer.close(index);
					        $.ajax({
					            type : "POST",
					            dataType:"json",
					            contentType: "application/json;charset=UTF-8",
					            url : "problem/updateProblemType",
					            async: false,
					            data : JSON.stringify(data),
					            success : function(result) {
					            	
					            },
					            error : function(e){
					                console.log(e.status);
					                console.log(e.responseText);
					            }
					        });
					   });
			    	}
			  });
		});
	</script>
</body>
</html>